package Messages;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JOptionPane;

import ocsf.server.ConnectionToClient;
import server.SQLConnection;
/**
 * This class implements the Add user to DB
 * @author Gabi Revah
 * @author Aviad Almog
 */
public class MessageAddUser {
	
	
	private ConnectionToClient client ;
	private String[] str; 
	
/**
 * Contractor of this class 
 * @param str
 * @param client
 */
	public MessageAddUser(String[] str, ConnectionToClient client){
		
		this.client= client ;
		this.str= str; 	
	}
	
	
	
/**
 * Adding user to the DB
 */
	public void AddUser(){
			try 
				{
					Statement stmt;
					SQLConnection conn = new  SQLConnection();
				
					java.sql.PreparedStatement ps = conn.getConnection().prepareStatement("SELECT `WorkerId` FROM db.workers s WHERE WorkerId='"+str[1]+"';");
						try {
		
								ResultSet rs= ps.executeQuery();
								if (!rs.next())
									{

										ps.executeUpdate("INSERT INTO db.workers (WorkerId,Password,FirstName,LastName,Email,Role,Connected) VALUES('"+str[1]+"','"+str[2]+"','"+str[3]+"','"+str[4]+"','"+str[5]+"','"+str[6]+"',0);");
										
										
											client.sendToClient("AddUser");
									}
								else 
									{
									
										client.sendToClient("fail");
										
									}
		  
							}
						catch (SQLException e)
						{	
							e.printStackTrace();
						}
				}
		  
		  catch (Exception ex )
		  	{
			  ex.printStackTrace();
		  	}
		
      
	}
	
	
}
